import time
from multiprocessing import Process


def f():
    count = 0
    while True:
        print("child process is running", count)
        time.sleep(2)
        count += 1


def cmd_ctrol_proce(proc):
    help = """
1 start 运行
2 is_alive 查询
3 terminate 终止进程
4 kill 发送信号
5 join 等待结束, 显示exitcode
6 close 关闭进程
q 退出
"""
    print(help)
    while True:
        cmd = input("cmd: ")
        if cmd == "1":
            proc.start()
            print("start")
        elif cmd == "2":
            print("is_alive", proc.is_alive())
        elif cmd == "3":
            proc.terminate()
            print("terminate")
        elif cmd == "4":
            proc.kill()
            print("kill")
        elif cmd == "5":
            proc.join()
            print("join")
            print("exitcode", proc.exitcode)
        elif cmd == "6":
            proc.close()
            print("close")
        elif cmd == "q":
            break
        else:
            print(help)


if __name__ == "__main__":
    p = Process(target=f)
    cmd_ctrol_proce(p)

""" 测试结果
terminate 终止进程, exitcode = -15

kill 终止进程, exitcode = -9

1.close 关闭 running的进程, 抛出异常
2.close 在 join/terminate 之后, 正常关闭
"""